package com.turbo.boot.crawler.spider.worker.impl.quota;

import com.alibaba.fastjson.JSON;
import com.turbo.boot.crawler.dao.dal.CrawlerResultDal;
import com.turbo.boot.crawler.dao.entity.CrawlerResultEntity;
import com.turbo.boot.crawler.spider.worker.BaseTurboCrawlerPageParser;
import com.xuxueli.crawler.annotation.PageFieldSelect;
import com.xuxueli.crawler.annotation.PageSelect;
import lombok.Getter;
import lombok.Setter;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 描述
 *
 * @author wuyn
 * @since 2024/4/19
 */
@Service("sofrCrawlerPageParser")
public class SofrCrawlerPageParser extends BaseTurboCrawlerPageParser<SofrCrawlerPageParser.PageVo> {

    @Resource
    private CrawlerResultDal crawlerResultDal;


    @Override
    public void parse(Document html, Element pageVoElement, PageVo pageVo) {
        // 解析封装 PageVo 对象
        String pageUrl = html.baseUri();
        // 忽略表头
        if ("SOFR".equals(pageVo.getSofr()) || pageVo.getSofr() == null) {
            return;
        }
        System.out.println(pageUrl + "：" + pageVo.toString());

        CrawlerResultEntity entity = buildCrawlerResultEntity();
        entity.setQuotaCode("SOFR");
        entity.setQuotaName("SOFR");
        entity.setQuotaValue(pageVo.getSofr());
        entity.setCollectedAt(new Date());

        crawlerResultDal.insert(entity);

    }


    @Setter
    @Getter
    @PageSelect(cssQuery = "#rates tbody tr")
    public static class PageVo {

        @PageFieldSelect(cssQuery = "td:nth-child(1)")
        private String date;

        @PageFieldSelect(cssQuery = "td:nth-child(2)")
        private String sofr;

        @PageFieldSelect(cssQuery = "td:nth-child(3)")
        private String history30Day;

        @PageFieldSelect(cssQuery = "td:nth-child(4)")
        private String history90Day;


        @PageFieldSelect(cssQuery = "td:nth-child(5)")
        private String history180Day;


        @Override
        public String toString() {
            return JSON.toJSONString(this);
        }
    }
}
